package com.base.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.common.util.IPageUtil;
import com.base.control.model.DictionariesDto;
import com.base.control.model.EsDictionariesDto;
import com.base.domain.mapper.DictionariesMapper;
import com.base.domain.module.po.Dictionaries;
import com.base.domain.module.vo.DictionariesVo;
import com.base.domain.module.vo.EsDictionariesVo;
import com.base.domain.service.IDictionariesService;
import com.bean.ObjectMapperUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cyj
 * @since 2021-12-24
 */
@Service
@Slf4j
public class DictionariesServiceImpl extends ServiceImpl<DictionariesMapper, Dictionaries> implements IDictionariesService {

    @Override
    public List<DictionariesVo> group(String group) {
        LambdaQueryWrapper<Dictionaries> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Dictionaries::getFgroup, group).orderByDesc(Dictionaries::getUpdateDate);
        List<Dictionaries> list = this.list(lambdaQueryWrapper);
        return ObjectMapperUtils.clones(list, DictionariesVo.class);
    }

    @Override
    public void add(DictionariesDto dictionariesDto) {
        Dictionaries dictionaries = ObjectMapperUtils.clone(dictionariesDto, Dictionaries.class);
        if (null != dictionaries) {
            this.save(dictionaries);
        }
    }

    @Override
    public void update(DictionariesDto dictionariesDto) {
        Dictionaries dictionaries = ObjectMapperUtils.clone(dictionariesDto, Dictionaries.class);
        if (null != dictionaries) {
            this.updateById(dictionaries);
        }
    }

    @Override
    public void delete(Integer id) {
        this.removeById(id);
    }

    @Override
    public IPage<EsDictionariesVo> keyword(String group, String keyword, Page page) {
        LambdaQueryWrapper<Dictionaries> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .eq(Dictionaries::getFgroup, group)
                .like(StringUtils.isNotEmpty(keyword), Dictionaries::getFvalue, keyword)
                .orderByDesc(Dictionaries::getUpdateDate);
        IPage page1 = this.page(page, lambdaQueryWrapper);
        return IPageUtil.copy(page1, EsDictionariesVo.class);
    }

    @Override
    public void addes(String group, List<EsDictionariesDto> esDictionariesDtos) {
        List<Dictionaries> dictionaries = esDictionariesDtos.stream().map(esDictionariesDto -> {
            Dictionaries clone = ObjectMapperUtils.clone(esDictionariesDto, Dictionaries.class);
            clone.setFgroup(group);
            clone.setFkey(esDictionariesDto.getFvalue());
            return clone;
        }).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(dictionaries)) {
            this.saveBatch(dictionaries);
        }
    }

    @Override
    public String addEsHead(String group, List<DictionariesVo> dictionariesVos, HttpServletResponse response) {
        DictionariesVo max = dictionariesVos.stream().max((e1, e2) -> e1.getUpdateDate().compareTo(e2.getUpdateDate())).get();
        log.info("es词库头 一个：{}", max);
        response.addHeader("Last-Modified", String.valueOf(max.getUpdateDate().getTime()));
        response.addHeader("ETag", max.getFvalue());
        return StringUtils.join(dictionariesVos.stream().map(DictionariesVo::getFvalue).distinct().collect(Collectors.toList()), "\n");
    }

}
